//
//  PictureViewController.m
//  TPYComputerNet
//
//  Created by 李迪 on 14-10-15.
//  Copyright (c) 2014年 李迪+赵俊飞+徐培帅. All rights reserved.
//

#import "PictureViewController.h"
#import "MmoTableViewCell.h"
#import "LiBaseTwoModel.h"
#import "NetworkHandler.h"
#import "UIButton+WebCache.h"
@interface PictureViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *data1;
@property(nonatomic, retain)NSMutableArray *data2;
@end

@implementation PictureViewController
- (void)dealloc
{
    [_tableView release];
    [_button release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.data2 = [NSMutableArray array];
    //设置标题
    self.navigationItem.title = @"图赏";
    //修改navigationBar的颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed: 0 / 255.0 green:255.0 / 255.0 blue: 225.0 / 255.0 alpha:1];
    //修改右键按钮
    UIImage *backImage = [UIImage imageNamed:@"TouXiang.png"];
    CGRect backframe = CGRectMake(0,0,54,40);
    UIButton *backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    [rightItem release];
    [self creatTabelView];
    [self creat];
    
}

-(void)creat
{

    [NetworkHandler connectNetGetDataWithURLString:@"http://agent1.pconline.com.cn:8941/photolib/iphone_cate_json.jsp?id=41" complention:^(id result) {
        _data1 = result;
        for (NSDictionary *dic in [result objectForKey:@"groups"]) {
            LiBaseTwoModel *model = [[LiBaseTwoModel alloc]init];
            model.cover = [dic objectForKey:@"cover"];
            model.name = [dic objectForKey:@"name"];
            model.picture = [dic objectForKey:@"photoCount"];
//            NSLog(@"%@",[dic objectForKey:@"photoCount"]);
            [self.data2 addObject:model];

        }
        [_tableView reloadData];

    }];
    
}
#pragma mark-
#pragma mark创建tableView
-(void)creatTabelView
{
    self.navigationController.navigationBar.translucent = NO;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 430 - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
}
#pragma mark-
#pragma marktableView实现的2个协议
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_data2.count == 0) {
        return 0;
    }
    return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MmoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[MmoTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]autorelease];
    }
    NSURL *url = [NSURL URLWithString:[_data2[indexPath.row ]cover]];
    [cell.button1 sd_setBackgroundImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
    cell.lable1.text = [_data2[indexPath.row]name];
    cell.lable2.text= [NSString stringWithFormat:@"%@张",[_data2[indexPath.row]picture]];

    return cell;
}
#pragma mark-
#pragma mark自定义cell大小
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
